package or.exa.cangguan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.common.Redis;
import or.exa.cangguan.entity.dto.EmployeeDto;
import or.exa.cangguan.entity.dto.ShelfDto;
import or.exa.cangguan.entity.dto.ShelfTypeDto;
import or.exa.cangguan.entity.po.Shelf;
import or.exa.cangguan.entity.vo.ShelfVo;
import or.exa.cangguan.mapper.AreaMapper;
import or.exa.cangguan.mapper.ShelfMapper;
import or.exa.cangguan.service.ShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static cn.hutool.poi.excel.sax.AttributeName.s;

@Slf4j
@Service
public class ShelfServiceImpl implements ShelfService {

    @Autowired
    private ShelfMapper shelfMapper;

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private Redis redis;

    //新增
    @Override
    public int addShelf(ShelfVo shelfVo) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        Shelf shelf = new Shelf();
        Integer areaId = null;
        if (shelfVo.getAreaName() != null && shelfVo.getAreaName()!= "") {
            areaId= areaMapper.selectIdByName(shelfVo.getAreaName());

        }
        Integer shelfId = null;
        if (shelfVo.getParentName() != null && shelfVo.getParentName()!= "") {
            shelfId = shelfMapper.selectIdByName(shelfVo.getParentName());

        }

        shelf.setParentId(shelfId);
        shelf.setAreaId(areaId);
        log.info("-------------------------------------"+shelfVo);
        BeanUtil.copyProperties(shelfVo, shelf);
        shelf.setUpdateBy(0);
        shelf.setCreateBy(0);
        shelf.setUpdateTime(date);
        shelf.setCreateTime(date);
        log.info(date.toString());

        log.info("-------------------------------------"+shelf);
        int insert = shelfMapper.insert(shelf);

        redis.del("shelfName");

        return insert   ;
    }


    //更新
    @Override
    public int updateShelf(ShelfVo shelfVo) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        Shelf shelf = new Shelf();
        Integer areaId = null;
        if (shelfVo.getAreaName() != null && shelfVo.getAreaName()!= "") {
            areaId= areaMapper.selectIdByName(shelfVo.getAreaName());

        }
        Integer shelfId = null;
        if (shelfVo.getParentName() != null && shelfVo.getParentName()!= "") {
            shelfId = shelfMapper.selectIdByName(shelfVo.getParentName());

        }

        shelf.setParentId(shelfId);
        shelf.setAreaId(areaId);
        log.info("-------------------------------------"+shelfVo);
        BeanUtil.copyProperties(shelfVo, shelf);
        shelf.setUpdateBy(0);
        shelf.setUpdateTime(date);
        log.info("-------------------------------------"+shelf);

        int i = shelfMapper.updateByPrimaryKeySelective(shelf);

        redis.del("shelfName");

        return i;
    }

    //删除
    @Override
    public int deleteShelf(Integer shelfId) {

        int i = shelfMapper.deleteByPrimaryKey(shelfId);
        int j = shelfMapper.deleteByParentId(shelfId);


        redis.del("shelfName");

        return i;
    }


    //查询
    @Override
    public Page searchShelf(ShelfVo shelfVo) {

        log.info("-------------------------------------"+shelfVo);
        Integer areaId=null;
        if (shelfVo.getAreaName() != null && shelfVo.getAreaName()!= "") {
            areaId = areaMapper.selectIdByName(shelfVo.getAreaName());
        }
        Page page = new Page();
        Shelf shelf= new Shelf();
        shelf.setShelfName(shelfVo.getShelfName());
        shelf.setType(shelfVo.getType());
        shelf.setAreaId(areaId);
        BeanUtil.copyProperties(shelfVo, page);
        int totalNum=shelfMapper.selectShelvesCount(shelf);
        page.setTotalNum(totalNum);
        List<ShelfDto> shelves = shelfMapper.selectShelves(shelf,page);
        page.setResultList(shelves);
        page.setTotalNum(totalNum);

        return page;
    }


    //查询所有货架的名称
    @Override
    public List<ShelfTypeDto> searchShelfName() {
        if (redis.idExists("shelfName")){
            return (List<ShelfTypeDto>) redis.get("shelfName");
        }
        else {
            List<ShelfTypeDto> shelfName = shelfMapper.selectShelfName();
            redis.set("shelfName", shelfName);
            return shelfName;
        }

    }
}
